<?php
namespace app\index\model;

class Form extends Base
{
	public function __construct()
	{
		 parent::__construct();
    }

    /**
     * @param array $termId     表单所属栏目id
     * @return array
     */
    public function addContent($termId)
    {
        $modelId = D('Term')->getModelId($termId);
        $tags    = D('TermModel')->getIdModelTags($modelId, 'content');
        $data    = $this->getPost($tags);
        $add['tags_value'] = json_encode($data);
        $add['term_id'] = $termId;
        $add['name'] = '用户留言';
        $add['status'] = '0';
        $add['create_time'] = time();
        return D('Content')->add($add);
    }

    /**
     * @param array $termId  表单所属栏目Id
     */
    public function addMessage($termId)
    {
        $messageModle = D('Message');
        $messageModle->create();
        $messageModle->create_time = time();
        return $messageModle->add();
    }

    public function getPost($tags)
    {
        $data = [];
        foreach($tags as $key=>$tag){
            if($tag['tag_type']=='webuploader-one')
                $data[$key] = $this->getUpload($key);
            elseif(I('?post.'.$key))
                $data[$key] = I('post.'.$key);
        }
        return $data;
    }

    public function getUpload($key)
    {
		 if (!empty($_FILES)) {
			 $upload = new \org\Upload();
			 $upload->maxSize  = 3145728 ;
			 $upload->saveRule = 'time';
			 $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg','zip','rar','doc','docx');
			 $upload->savePath = "public/Uploads/";
			 
			 if(false == $info = $upload->uploadOne($_FILES[$key])) 
			 {
                 return '';
			 }else{
                 return $info[0]['savename'];
			 }
		 }else {
             return '';
    	 }
    }
}

